package cn.iocoder.yudao.module.shop.service.memberpay;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.yudao.module.shop.controller.admin.memberpay.vo.*;
import cn.iocoder.yudao.module.shop.dal.dataobject.memberpay.ShopMemberPayDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;

import cn.iocoder.yudao.module.shop.convert.memberpay.ShopMemberPayConvert;
import cn.iocoder.yudao.module.shop.dal.mysql.memberpay.ShopMemberPayMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.shop.enums.ErrorCodeConstants.*;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;

/**
 * 每支价格 Service 实现类
 *
 * @author 曹建伟
 */
@Service
@Validated
public class ShopMemberPayServiceImpl implements ShopMemberPayService {

    @Resource
    private ShopMemberPayMapper memberPayMapper;

    @Override
    public Long createMemberPay(ShopMemberPayCreateReqVO createReqVO) {
        // 插入
        ShopMemberPayDO memberPay = ShopMemberPayConvert.INSTANCE.convert(createReqVO);
        memberPayMapper.insert(memberPay);
        // 返回
        return memberPay.getId();
    }

    @Override
    public void updateMemberPay(ShopMemberPayUpdateReqVO updateReqVO) {
        // 校验存在
        validateMemberPayExists(updateReqVO.getId());
        // 更新
        ShopMemberPayDO updateObj = ShopMemberPayConvert.INSTANCE.convert(updateReqVO);
        memberPayMapper.updateById(updateObj);
    }

    @Override
    public void deleteMemberPay(Long id) {
        // 校验存在
        validateMemberPayExists(id);
        // 删除
        memberPayMapper.deleteById(id);
    }

    private void validateMemberPayExists(Long id) {
        if (memberPayMapper.selectById(id) == null) {
            throw exception(MEMBER_PAY_NOT_EXISTS);
        }
    }

    @Override
    public ShopMemberPayDO getMemberPay(Long id) {
        validateMemberPayExists(id);
        return memberPayMapper.selectById(id);
    }

    @Override
    public List<ShopMemberPayDO> getMemberPayList(Collection<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return ListUtil.empty();
        }
        return memberPayMapper.selectBatchIds(ids);
    }

    @Override
    public List<ShopMemberPayDO> getMemberPayList() {
        return memberPayMapper.selectList();
    }

    @Override
    public PageResult<ShopMemberPayDO> getMemberPayPage(ShopMemberPayPageReqVO pageReqVO) {
        return memberPayMapper.selectPage(pageReqVO);
    }

    @Override
    public List<ShopMemberPayDO> getMemberPayList(ShopMemberPayExportReqVO exportReqVO) {
        return memberPayMapper.selectList(exportReqVO);
    }

}
